﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using mmoss.application.classes;
using mmoss.configuration;
using System.Windows.Forms;

namespace mmoss.application
{
    public partial class CashRegister : Form
    {
        private decimal _cashTendered=0;
        private decimal _changeNeeded = 0;
        private bool alreadyFocused;
        public decimal ChangeNeeded
        {
            get { return _changeNeeded; }
            set { _changeNeeded = value; }
        }
        public decimal CashTendered
        {
            get { return _cashTendered; }
            set { _cashTendered = value; }
        }
        decimal CartTotal { get; set; }
        public CashRegister()
        {
            InitializeComponent();
        }
        public CashRegister(decimal l_total)
        {
            InitializeComponent();
            CartTotal = l_total;
        }


        void CashRegister_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (((int)e.KeyChar) == 46)
            {
                if (!txtTendered.Text.Contains("."))
                {
                    txtTendered.Text += e.KeyChar;
                }
            }
            if (((int)e.KeyChar) > 47 && ((int)e.KeyChar) < 58)
            {
                txtTendered.Text += e.KeyChar;
            }
        }

        private void btnRegisterDecimal_Click(object sender, EventArgs e)
        {
            if (!txtTendered.Text.Contains("."))
            {
                txtTendered.Text += ".";
            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            //amount accepted.  Set record and return.
            CashTendered = decimal.Parse(txtTendered.Text);
            ChangeNeeded = decimal.Parse(txtChange.Text);
            this.Close();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void CashRegister_Load(object sender, EventArgs e)
        {
            //set the events
            this.KeyPress += new KeyPressEventHandler(CashRegister_KeyPress);
            btnRegisterOne.Click += delegate(object s2, EventArgs e2)
            { txtTendered.Text += "1"; };
            btnRegisterTwo.Click += delegate(object s2, EventArgs e2)
            { txtTendered.Text += "2"; };
            btnRegisterThree.Click += delegate(object s2, EventArgs e2)
            { txtTendered.Text += "3"; };
            btnRegisterFour.Click += delegate(object s2, EventArgs e2)
            { txtTendered.Text += "4"; };
            btnRegisterFive.Click += delegate(object s2, EventArgs e2)
            { txtTendered.Text += "5"; };
            btnRegisterSix.Click += delegate(object s2, EventArgs e2)
            { txtTendered.Text += "6"; };
            btnRegisterSeven.Click += delegate(object s2, EventArgs e2)
            { txtTendered.Text += "7"; };
            btnRegisterEight.Click += delegate(object s2, EventArgs e2)
            { txtTendered.Text += "8"; };
            btnRegisterNine.Click += delegate(object s2, EventArgs e2)
            { txtTendered.Text += "9"; };
            btnRegisterZero.Click += delegate(object s2, EventArgs e2)
            { txtTendered.Text += "0"; };
            btnRegisterDel.Click += delegate(object s2, EventArgs e2)
            { txtTendered.Text = txtTendered.Text.Substring(1, txtTendered.Text.Length - 1); };


            txtTotal.Text = CartTotal.ToString();
            txtTendered.Text = (Math.Truncate(decimal.Parse(txtTotal.Text)) + 1).ToString();
            calcDiff();

            //events
            txtTendered.Leave += new EventHandler(txtTendered_Leave);
            txtTendered.Enter += new EventHandler(mmossEvents.txtHandler); //txtTendered_Enter);

            //set the focus on tendered row
            txtTendered.GotFocus += new EventHandler(txtTendered_GotFocus);
            txtTendered.MouseUp += new MouseEventHandler(txtTendered_MouseUp);
        }

        void txtTendered_MouseUp(object sender, MouseEventArgs e)
        {
            if (!alreadyFocused && ((TextBox)sender).SelectionLength == 0)
            {
                alreadyFocused = true;
                ((TextBox)sender).SelectAll();
            }
        }

        void txtTendered_GotFocus(object sender, EventArgs e)
        {
            if (MouseButtons == MouseButtons.None)
            {
                ((TextBox)sender).SelectAll();
                alreadyFocused = true;
            }
        }


        void txtTendered_Leave(object sender, EventArgs e)
        {
            alreadyFocused = false;
            calcDiff();
        }

        
        private void calcDiff()
        {
            decimal l_total = decimal.Parse(txtTotal.Text);
            decimal l_tendered = decimal.Parse(txtTendered.Text);
            decimal l_diff = l_tendered - l_total;
            txtChange.Text = l_diff.ToString();
        }


    }
}

